package com.example.houseweb.controller;

import com.example.housebiz.service.AgencyService;
import com.example.housebiz.service.CityService;
import com.example.housebiz.service.HouseService;
import com.example.housebiz.service.RecommendService;
import com.example.housecommon.constants.CommonConstant;
import com.example.housecommon.constants.HouseUserType;
import com.example.housecommon.model.House;
import com.example.housecommon.model.HouseUser;
import com.example.housecommon.model.User;
import com.example.housecommon.model.UserMsg;
import com.example.housecommon.page.PageData;
import com.example.housecommon.page.PageParams;
import com.example.housecommon.result.ResultMsg;
import com.example.houseweb.intercept.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author ：Anitaxiaofeng
 * @description：
 * @create 2018-05-30
 **/
@Controller
public class HouseController {

    @Autowired
    private HouseService houseService;


    @Autowired
    private AgencyService agencyService;

    @Autowired
    private RecommendService recommendService;

    @Autowired
    private CityService cityService;

    /**
     * 1.支持分页
     * 2.支持小区搜索、类型搜索
     * 3.支持排序
     * 4.支持展示图片、价格、标题、地址等信息
     * @return
     */
    @RequestMapping("/house/list")
    public String houseList(Integer pageSize, Integer pageNum, House query, ModelMap modelMap) {
        PageData<House> housePageData = houseService.queryHouse(query, PageParams.build(pageSize,pageNum));
        List<House> hotHouses =  recommendService.getHotHouse(CommonConstant.RECOM_SIZE);
        modelMap.put("recomHouses", hotHouses);
        modelMap.put("ps",housePageData);
        modelMap.put("vo",query);
        return "house/listing";
    }

    /**
     * 查询房屋详情
     * 查询关联经纪人
     * @param id
     * @return
     */
    @RequestMapping("/house/detail")
    public String houseDetail(Long id,ModelMap modelMap) {
        House house = houseService.queryOneHouse(id);
        HouseUser houseUser = houseService.getHouseUser(id);
        recommendService.increase(id);
        if(houseUser.getUserId()!=null && !houseUser.getUserId().equals(0)) {
            modelMap.put("agent",agencyService.getAgentDetail(houseUser.getUserId()));
        }

        //TODO commentList
        List<House> rcHouses =  recommendService.getHotHouse(CommonConstant.RECOM_SIZE);
        modelMap.put("recomHouses", rcHouses);
        modelMap.put("house",house);
        return "house/detail";
    }

    @RequestMapping("/house/leaveMsg")
    public String houseMsg(UserMsg userMsg) {
        houseService.addUserMsg(userMsg);
        return "redirect:/house/detail?id="+userMsg.getHouseId()+ ResultMsg.successMsg("留言成功").asUrlParams();
    }

    @RequestMapping("/house/toAdd")
    public String toAdd(ModelMap modelMap) {
        modelMap.put("citys",cityService.getAllCitys());
        modelMap.put("communitys",houseService.getAllCommunitys());
        return "house/add";
    }

    /**
     * 获取用户
     * 设置房产状态
     * 添加房产
     * @param house
     * @return
     */
    @RequestMapping("/house/add")
    public String doAdd(House house) {
        User user = UserContext.getUser();
        house.setState(CommonConstant.HOUSE_STATE_UP);
        houseService.addHouse(house,user);
        return "redirect:/house/ownlist";
    }

    @RequestMapping("/house/ownlist")
    public String ownlist(House house,Integer pageNum,Integer pageSize,ModelMap modelMap) {
        User user = UserContext.getUser();
        house.setUserId(user.getId());
        house.setBookmarked(false);
        modelMap.put("ps",houseService.queryHouse(house,PageParams.build(pageSize,pageNum)));
        modelMap.put("pageType","own");
        return "house/ownlist";
    }

    /**
     * 评分
     * @param rating
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("/house/rating")
    public ResultMsg houseRate(Double rating,Long id) {
        houseService.updateRating(id,rating);
        return ResultMsg.successMsg("ok");
    }


    /**
     * 收藏
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("/house/bookmark")
    public ResultMsg bookmark(Long id) {
        User user = UserContext.getUser();
        houseService.bindUser2House(id,user.getId(),true);
        return ResultMsg.successMsg("ok");
    }

    @ResponseBody
    @RequestMapping("/house/unbookmark")
    public ResultMsg unbookmark(Long id) {
        User user = UserContext.getUser();
        houseService.unbindUser2House(id,user.getId(), HouseUserType.BOOKMARK);
        return ResultMsg.successMsg("ok");
    }

    @RequestMapping("/house/del")
    public String delsale(Long id ,String pageType) {
        User user = UserContext.getUser();
        houseService.unbindUser2House(id,user.getId(),pageType.equals("own")?HouseUserType.SALE:HouseUserType.BOOKMARK);
        return "redirect:/house/ownlist";
    }


    /**
     * 收藏列表
     * @param house
     * @param pageNum
     * @param pageSize
     * @param modelMap
     * @return
     */
    @RequestMapping("/house/bookmarked")
    public String bookmarked(House house,Integer pageNum, Integer pageSize, ModelMap modelMap) {
        User user = UserContext.getUser();
        house.setBookmarked(true);
        house.setUserId(user.getId());
        modelMap.put("ps",houseService.queryHouse(house,PageParams.build(pageSize,pageNum)));
        modelMap.put("pageType","book");
        return "house/ownlist";
    }

}





